package com.ldxiwxr.controller.controller.article;
import com.ldxiwxr.common.common.New;
import com.ldxiwxr.common.common.Result;
import com.ldxiwxr.common.utils.EasyAPIResultUtils;
import com.ldxiwxr.system.service.IArticleService;
import com.ldxiwxr.system.vo.article.ArticleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "/IndexPageController")
@RequestMapping("/articleIndex")
public class IndexPageController {

    @Resource
    private IArticleService articleService;

    @ApiOperation("获取一言")
    @GetMapping("/yiyan")
    public Result<?> yiyan(){
        //返回一言
        return Result.data(EasyAPIResultUtils.YiYan());
    }

    @ApiOperation("获取新闻")
    @GetMapping("/getNews")
    public Result<?> getNews(){
        //根据用户地区查看新闻
        List<New> newList = EasyAPIResultUtils.getNewList();
        //判断是否查询到了内容
        if (newList != null){
            return Result.data(newList);
        }
        return Result.fail("查询不到新闻内容");
    }

    @ApiOperation("获取文章列表")
    @GetMapping("/getArticleList")
    public Result<?> getArticleList(String title){
        //返回文章VO列表
        return Result.data(articleService.getArticleList(7,title).stream().map((item) ->{
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(item,articleVO);
            return articleVO;
        }).collect(Collectors.toList()));
    }
}
